package project.layer.ant;

import java.util.ArrayList;
import java.util.List;

import framework.events.EventBroker;
import project.events.antEvents.SearchingExplorationAntCreatedEvent;
import project.events.antEvents.SearchingExplorationAntDiedEvent;
import project.layer.agents.ReportReceiver;
import project.layer.agents.TruckAgent;
import project.layer.physical.Crossroad;
import project.layer.physical.Path;
import project.layer.physical.Road;

public class SearchingExplorationAnt extends ExplorationAnt {
	
	private boolean hasPickedUpPackage = false;
	private int pickedUpPackageId = 0;
	
	public SearchingExplorationAnt(TruckAgent sendingAgent, Crossroad location, int maxHops, int nbOfPnDSequencesLeft){
		super(sendingAgent, location, maxHops, nbOfPnDSequencesLeft);
		followedPath.addCrossroadToPath(location);
		EventBroker.getEventBroker().notifyAll(new SearchingExplorationAntCreatedEvent(this));
	}
	
	private SearchingExplorationAnt(TruckAgent sendingAgent, ReportReceiver receiver,  ReportReceiver earlierReceiver,Crossroad location, int maxHops, int currentHopCount, Path currentPath,int nbOfPnDSequencesLeft, Road lastRoad,long travelledDistance, Path previousPath){
		super(sendingAgent,receiver, earlierReceiver, location,maxHops,currentHopCount, currentPath, nbOfPnDSequencesLeft, lastRoad,travelledDistance, previousPath);
		EventBroker.getEventBroker().notifyAll(new SearchingExplorationAntCreatedEvent(this));
	}
	
	/**
	 * Constructor of a SearchingExplorationAnt used upon creation of a new ExplorationAnt at the dropofflocation of a previous PND sequence
	 */
	public SearchingExplorationAnt(TruckAgent sendingAgent, ReportReceiver receiver, ReportReceiver earlierReceiver, Crossroad location, int maxHops,Path pathofPreviousPnDSequences,int nbOfPnDSequencesLeft ){
		super(sendingAgent,receiver, earlierReceiver, location, maxHops,pathofPreviousPnDSequences ,nbOfPnDSequencesLeft);
		followedPath.addCrossroadToPath(location);
		EventBroker.getEventBroker().notifyAll(new SearchingExplorationAntCreatedEvent(this));
	}
	
	public void cloneAndSpread(){
		if(getRemainingHops() <= 1){
			// hier die() niet oproepen want een ant kan maar 1 keer sterven, dit gebeurt nu al in processTick()!
//			die();
			
			
		}
		else{
			
			for(Road road : getLocation().getOutgoingConnections()){
				Crossroad cr = road.getOtherConnector(getLocation());
				if(!getPath().contains(cr)){
					Path clone = getPath().getClone();
					clone.addCrossroadToPath(cr);
					SearchingExplorationAnt newAnt = new SearchingExplorationAnt(getSendingAgent(), getReceiver(), getEarlierReceiver(), cr, getMaxHops(), getCurrentHopCount() +1 , clone, getNbOfPnDSequencesLeft(), road,getTravelledDistance() + road.getLength(), getPathOfPreviousPnDSequences());
				}
				
				
			}
		}
	}
	
	
	
	public void processAntTick(){
		// zorg ervoor dat voor een ant de methode die() niet twee of meerdere keren wordt opgeroepen!!
	
		
		List<Integer> possibleDestinations = getLocation().getDataHolder().getFeasiblityOriginIDs();
		if(possibleDestinations.size() > 0){
			for(Integer destID : possibleDestinations){
				if(!getDestinationsOfPreviousPnDSequences().contains(destID)){	
					Road nextRoad = getLocation().getDataHolder().getRoadWithMostFeasibilityPheromones(getDestinationsOfPreviousPnDSequences(), destID);
					Crossroad nextCrossroad = nextRoad.getOtherConnector(getLocation());
					FollowingExplorationAnt followingExplorationAnt = new FollowingExplorationAnt(getSendingAgent(), getReceiver(), getEarlierReceiver(), nextCrossroad, 20, 0, getPath(), 
							getNbOfPnDSequencesLeft(), nextRoad, getTravelledDistance() + nextRoad.getLength(), getPathOfPreviousPnDSequences(), destID);
				}
			}
		}
		
		cloneAndSpread();
		die();
	}
	

	

	
	public boolean hasPickedUpPackage() {
		return hasPickedUpPackage;
	}

	public int getPickedUpPackageId() {
		return pickedUpPackageId;
	}

	@Override
	protected void createDieEvent() {
		EventBroker.getEventBroker().notifyAll(new SearchingExplorationAntDiedEvent(this));
		
	}


}
